package com.zprmb.udzt;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
/**
 * 我的二维码
 * @author shenshao
 *
 */
public class My_QR extends BaseActivity implements OnClickListener{
	private ImageView img_back;
	private TextView user_code;
	private RelativeLayout rl_back;
	private TextView tv_customer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_qr);
        BbtApplication.getInstance().addActvity(this);
        ImageView iv = (ImageView) findViewById(R.id.iv);
        img_back=(ImageView)findViewById(R.id.img_back);
        user_code=(TextView)findViewById(R.id.user_code);
        user_code.setText("我的邀请码"+getShare().getString("usercode", ""));
        rl_back=(RelativeLayout)findViewById(R.id.rl_back);

tv_customer=(TextView)findViewById(R.id.enterprise_tel);
tv_customer.setOnClickListener(this);
		rl_back.setOnClickListener(this);
        img_back.setOnClickListener(this);
        try {

            Bitmap bit = Create2DCode(Constant.URL+"/h5/spreadReg?ref="+getShare().getString("usercode", ""));
            iv.setImageBitmap(bit);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	BbtApplication.getInstance().removeActvity(this);
    }
    public Bitmap Create2DCode(String str) throws WriterException {
        //鐢熸垚浜岀淮鐭╅樀,缂栫爜鏃舵寚瀹氬ぇ灏�涓嶈鐢熸垚浜嗗浘鐗囦互鍚庡啀杩涜缂╂斁,杩欐牱浼氭ā绯婂鑷磋瘑鍒け璐�
        BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300, 300);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        //浜岀淮鐭╅樀杞负涓�淮鍍忕礌鏁扮粍,涔熷氨鏄竴鐩存í鐫�帓浜�
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if(matrix.get(x, y)){
                    pixels[y * width + x] = Color.rgb(29, 165, 246);
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //閫氳繃鍍忕礌鏁扮粍鐢熸垚bitmap,鍏蜂綋鍙傝�api
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.img_back:
			finish();
			break;
		case R.id.rl_back:
			finish();
			break;

		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		}
	}
}
